package com.jf3q.common.es.repository;

import com.jf3q.common.es.entity.CourseEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Highlight;
import org.springframework.data.elasticsearch.annotations.HighlightField;
import org.springframework.data.elasticsearch.annotations.HighlightParameters;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CourseEntityRepository extends ElasticsearchRepository<CourseEntity, String> {

    @Query("{\n" +
            "     \"multi_match\": {\n" +
            "       \"query\":\"?0\",\n" +
            "       \"fields\":\n" +
            "       [\n" +
            "         \"title\",\"subjectName\",\"subjectParentName\",\"description\"\n" +
            "         ]\n" +
            "    }\n" +
            "  }")
    @Highlight(
            fields = {
                    @HighlightField(name = "title"),
                    @HighlightField(name = "subjectName"),
                    @HighlightField(name = "subjectParentName"),
                    @HighlightField(name = "description")
            },
            parameters=@HighlightParameters(preTags = "<font color='red'>", postTags = "</font>")
    )

    /*
    * 返回List<SearchHit<CourseEntity>>的原因是为了提供比仅仅返回实体列表更多的信息。
    * SearchHit类包含了关于每个搜索结果的详细信息
    * */
    List<SearchHit<CourseEntity>> searchByHit(String keyword);


    //根据二级分类id查找并分页展示
    Page<CourseEntity> findBySubjectId (String subjectId,Pageable pageable);
    //根据一级分类id查找并分页展示
    Page<CourseEntity> findBySubjectParentId(String subjectParentId,Pageable pageable);




}
